package com.legrand.user.controller;


import com.legrand.core.exception.CustomizeException;
import com.legrand.core.permission.RolePermission;
import com.legrand.core.util.BaseResponse;
import com.legrand.security.utils.RequestUtil;
import com.legrand.user.service.UserService;
import com.legrand.user.dto.UserNamePasswordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.RolesAllowed;

/**
 * @author xiah
 * @date 2018/4/26 16:24
 */
@RestController
@RequestMapping("/api/archive")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * @author xiah
     * 2018-07-10 09:09:36
     * 判断账户是否存在
     */
    @GetMapping(value = "/checkUserName")
    public BaseResponse registerByUserNameAndPassword(@RequestParam String userName) {
        return userService.getByUserName(userName);
    }

    /**
     * @author xiah
     * 2018-07-10 09:09:36
     * 用户账号密码注册
     */
    @PostMapping(value = "/user/register")
    public BaseResponse registerByUserNameAndPassword(@RequestBody UserNamePasswordDTO userNamePasswordDTO) {
        if (userNamePasswordDTO.getUserName() == null || userNamePasswordDTO.getPassword() == null || userNamePasswordDTO.getRePassword() == null) {
            throw new CustomizeException("缺少必填参数");
        }
        return userService.registerByUserNameAndPassword(userNamePasswordDTO);
    }


    /**
     * @author xiah
     * 2018-07-10 09:09:36
     * 用户账号密码登录
     */
    @PostMapping(value = "/user/login")
    public BaseResponse loginByUserNameAndPassword(@RequestBody UserNamePasswordDTO userNamePasswordDTO) {
        if (userNamePasswordDTO.getUserName() == null || userNamePasswordDTO.getPassword() == null) {
            throw new CustomizeException("缺少必填参数");
        }
        return userService.loginByUserNameAndPassword(userNamePasswordDTO);
    }

    /**
     * @author xiah
     * 2018-07-10 09:09:36
     * 用户账号密码登录
     */
    @PostMapping(value = "/sec/getUserName")
    @RolesAllowed({"ROLE_USER"})
    public BaseResponse getUserName() {
        return new BaseResponse(RequestUtil.getUserId());
    }

    /*@Autowired
    private UserService userService;

    @RequestMapping(value = "/sec/test")
    @ResponseBody
    public BaseResponse test() {
        String name = UserUtil.getUserName();
        return new BaseResponse(name);

    }

    *//**
     * @author xiah
     * description: 用户通过手机号和验证码获得token，refresh_token等信息
     * date: 2018/4/26
     *//*
    @RequestMapping(value = "/sec/getTokeByMobile", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse getTokeByMobile(@RequestParam String mobilePhone, @RequestParam String code) {
        if (!PhoneAndIDcardCheckUtil.isTelephone(mobilePhone)) {
            throw new ExceptionBase(122, "phoneNumber is wrong");
        }
        *//*登陆成功将用户信息保存到UserUtil*//*
        String name = UserUtil.getUserName();
        return new BaseResponse(name);
        //return new BaseResponse(JwtToken.creatTokenMap());
        //userService.getUser();
        //return "";
    }

    *//**
     * @author xiah
     * description: 用户通过手机号和验证码获得token，refresh_token等信息
     * date: 2018/4/26
     *//*
    @RequestMapping(value = "/refreshToke")
    @ResponseBody
    public BaseResponse refreshToke(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getHeader("refresh_token");
        String refresh_token = request.getHeader("refresh_token");
        Optional<User> uu = Optional.of(new User());
        if (refresh_token == null) {
            throw new ExceptionBase(122, "phoneNumber is wrong");
        }
        if (!JwtToken.checkToken(TokenUtil.interceptToken(refresh_token))) {
            response.setStatus(500);
            throw new ExceptionBase(122, "phoneNumber is wrong");
        }
        return new BaseResponse(JwtToken.createToken());
        //userService.getUser();
        //return "";

    }*/
}
